package com.town.garden.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DatePattern;
import com.town.garden.entiy.Result;
import com.town.garden.service.ZLMediaKitService;
import com.town.garden.template.S3Template;
import com.town.garden.utils.DateUtils;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

/**
 * @Description
 * @E-mail cwm3412638@163.com
 * @Author cwm
 * @Date 2022/5/26
 */
@RestController
@RequestMapping("/live")
@AllArgsConstructor
public class LiveBroadCastController {
    private final ZLMediaKitService zlMediaKitService;
    private final S3Template s3Template;

    @GetMapping("/list")
    public Result getLiveList() {
        return Result.ok(zlMediaKitService.getMediaList());
    }

    @GetMapping("/delete/minio")
    public void deleteMinio(@RequestParam("time") String time) {
        List<String> strings = this.s3Template.listByBucketName("zy-storage");
        if (CollectionUtil.isNotEmpty(strings)) {
            strings.stream().forEach(item -> {
                String[] split = item.split("-");
                String minioTime = split[0];
                Date date = null;
                try {
                    date = DateUtils.parseDate(minioTime, DatePattern.CHINESE_DATE_TIME_PATTERN);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                String s = DateUtils.parseDateToStr(DatePattern.NORM_DATE_PATTERN, date);
                if (s.equals(time)) {
                    this.s3Template.delete("zy-storage", item);
                }
            });
        }
    }
}
